/*
 * Copyright (C) 2004-2006 nickyb@users.sourceforge.net
 * 
 * This file is part of FreeQueryBuilder (http://querybuilder.sourceforge.net)
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package net.sourceforge.querybuilder;

import java.awt.Color;
import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;

import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.UIManager;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.TableColumnModelEvent;
import javax.swing.event.TableColumnModelListener;

import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableColumn;

public class QueryListbar extends JScrollPane implements TableColumnModelListener
{
	private QueryBuilder builder;
	private JTable bar;
	
	public QueryListbar(QueryBuilder builder)
	{
		this.builder = builder;
		
		setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_NEVER);
		
		setViewportView(bar = new JTable(0,0));
		getViewport().setBackground(Color.gray);
		
		bar.getTableHeader().setDefaultRenderer(new DefaultTableCellRenderer()
		{
			public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
			{
				super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
				
				setBorder(UIManager.getBorder("TableHeader.cellBorder"));
				setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
				setToolTipText(value.toString());
				setOpaque(false);
				
				return this;
			}
		});
		
		bar.getTableHeader().setCursor(new Cursor(Cursor.HAND_CURSOR));
		bar.getTableHeader().setBackground(new Color(255,255,225));
		bar.getColumnModel().addColumnModelListener(this);
	}
	
	public Dimension getPreferredSize()
	{
		int h = this.getFontMetrics(this.getFont()).getHeight();
		h += this.getFontMetrics(this.getFont()).getDescent();
		
		return new Dimension(0,h);
	}

	void addColumn(QueryTokens.Column column)
	{
		addItem(new ColumnItem(column));
	}

	void addExpression(String indentifier, String value)
	{
		addItem(new ExpressionItem(indentifier, value));
	}

	private void addItem(ExpressionItem item)
	{
		bar.getTableHeader().getColumnModel().addColumn(item);
		
		String display = item.getHeaderValue().toString();
		if(item instanceof ColumnItem)
		{
			display = ((ColumnItem)item).querytoken.toString(QueryBuilder.useIdentifierQuote,false);
			builder.clauses.group.addColumn(display);
		}
		builder.clauses.order.addExpression(display);
		builder.syntax.refresh();
	}
	
	boolean containsItem(String expressionKey)
	{
		return getItem(expressionKey)!=null;
	}
	
	int getItemCount()
	{
		return bar.getTableHeader().getColumnModel().getColumnCount();
	}
	
	ExpressionItem getItem(String indentifier)
	{
		try
		{
			int index = bar.getTableHeader().getColumnModel().getColumnIndex(indentifier);
			return getItemAt(index);
		}
		catch(IllegalArgumentException iae)
		{
		}
		
		return null;
	}

	ExpressionItem getItemAt(int index)
	{
		return (ExpressionItem)bar.getTableHeader().getColumnModel().getColumn(index);
	}

	void setExpression(String indentifier, String value)
	{
		ExpressionItem item = this.getItem(indentifier);
		builder.clauses.order.expressionChanged(item.getIdentifier().toString(), value);
		item.setHeaderValue(value);
		
		this.repaint();
	}

	void removeItem(String expressionKey)
	{
		ExpressionItem item = this.getItem(expressionKey);
		if(item == null) return;
		
		bar.getTableHeader().getColumnModel().removeColumn(item);
		
		String display = item.getHeaderValue().toString();
		if(item instanceof ColumnItem)
		{
			display = ((ColumnItem)item).querytoken.toString(QueryBuilder.useIdentifierQuote,false);
			builder.clauses.group.removeColumn(display);
		}
		builder.clauses.order.removeExpression(display);
		builder.syntax.refresh();
	}
	
	public void columnAdded(TableColumnModelEvent e)
	{
	}

	public void columnMarginChanged(ChangeEvent e)
	{
	}

	public void columnMoved(TableColumnModelEvent e)
	{
		builder.syntax.refresh();
	}

	public void columnRemoved(TableColumnModelEvent e)
	{
	}

	public void columnSelectionChanged(ListSelectionEvent e)
	{
	}
	
	class ExpressionItem extends TableColumn
	{
		ExpressionItem(String value)
		{
			this(value,value);
		}
		
		ExpressionItem(String indentifier, String value)
		{
			setIdentifier(indentifier);
			setHeaderValue(value);
		}
	}
	
	class ColumnItem extends ExpressionItem
	{
		QueryTokens.Column querytoken;
				
		ColumnItem(QueryTokens.Column querytoken)
		{
			super(querytoken.toString(false,false));
			this.querytoken = querytoken;
		}
		
		public Object getHeaderValue()
		{
			return querytoken.toString(false,true);
		}
	}
}
